﻿using Bentley.CifNET.GeometryModel;
using Bentley.CifNET.SDK;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BIM = Bentley.Interop.MicroStationDGN;
using BMI = Bentley.MstnPlatformNET.InteropServices;

namespace CheccCoverplateCulvertAssemble.Conmmon
{
    internal class ORDHelp
    {
        //public static ConsensusConnection sdkConn = new ConsensusConnection(Session.Instance.GetActiveDgnModel());

        private static string workSpacePath;

        public static string WorkSpacePath
        {
            get
            {
                return ConfigurationManager.GetVariable("Checc_CulvertCustomPath");
            }
            set
            {
                workSpacePath = value;
            }
        }


        /// <summary>
        /// 获取米点
        /// </summary>
        /// <param name="uorpoint"></param>
        /// <returns></returns>
        public static DPoint3d GetMeterPoint(DPoint3d uorpoint)
        {
            return uorpoint * (1 / UorPerMeter());
        }

        /// <summary>
        /// 获取像素点
        /// </summary>
        /// <param name="meterpoint"></param>
        /// <returns></returns>
        public static DPoint3d GetUORPoint(DPoint3d meterpoint)
        {
            return meterpoint * UorPerMeter();
        }

        /// <summary>
        /// 获取当前像素
        /// </summary>
        /// <returns></returns>
        public static double UorPerMeter()
        {
            ModelInfo modelInfo = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel().GetModelInfo();
            double uorsPerMeter = modelInfo.UorPerMeter;
            return uorsPerMeter;
        }

        public static List<LinearEntity2dInPlan> GetAllAlignmentsInModel(Bentley.CifNET.GeometryModel.GeometricModel activeGeometricModel)
        {
            List<LinearEntity2dInPlan> linearEntity2dInPlans = new();
            if (null == activeGeometricModel)
            {
                linearEntity2dInPlans = null;
            }

            List<Bentley.CifNET.GeometryModel.GeometricModel> geometricModels = new();
            geometricModels.AddRange(activeGeometricModel.GetReferenceModels().ToList());
            geometricModels.Add(activeGeometricModel);
            foreach (Bentley.CifNET.GeometryModel.GeometricModel geometricModel in geometricModels)
            {
                if (null == geometricModel.ThreeDSpace)
                {
                    continue;
                }

                LinearEntityNameSet linearEntityNameSet = geometricModel.LinearEntityNameSet;
                if (null == linearEntityNameSet)
                {
                    continue;
                }
                foreach (LinearEntity entity in linearEntityNameSet.GetEntities())
                {
                    if (entity is LinearEntity2dInPlan && entity.IsNamed)
                    {
                        linearEntity2dInPlans.Add(entity as LinearEntity2dInPlan);
                    }
                }
            }
            return linearEntity2dInPlans;
        }

        public static List<string> GetDgnFileModels(string dgnFileName)
        {
            string dgnFilePath = WorkSpacePath + @"Standards\Cell\圆管涵\" + dgnFileName;
            DgnDocument document = DgnDocument.CreateForLocalFile(dgnFilePath);
            DgnFileOwner owner = DgnFile.Create(document, DgnFileOpenMode.ReadOnly);
            DgnFile cellFile = owner.DgnFile;
            cellFile.LoadDgnFile(out StatusInt fileLoadstatus);
            if (fileLoadstatus == StatusInt.Error)
            {
                return null;
            }
            var models = cellFile.GetModelIndexCollection();
            return models.ToList().Select(s => s.Name).Where(w => w.ToString() != "Default").ToList();
        }

        public static void FitSelectView(int vieNo, bool isMaximize, bool isFit, bool isActive)
        {
            BIM.Application myApp = BMI.Utilities.ComApp;
            BIM.Views views = myApp.ActiveDesignFile.Views;
            BIM.View vie = views[vieNo];
            if (isMaximize == true)
                vie.Maximize();
            if (isFit == true)
                vie.Fit(true);
            if (isActive == true)
                vie.Select();
            vie.Redraw();
        }

    }
}
